﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class cargoinfomanager : Form
    {
        public cargoinfomanager()
        {
            InitializeComponent();
            // 读取信息
        }

        private void cargoinfomanager_Load(object sender, EventArgs e)
        {
            Read_DB_Fill_Grid("cargoinfotable",dataGridView1);
            Set_Color(dataGridView1);
        }

        private void Set_Color(DataGridView datagridview1)
        {
            for (int i = 0; i < datagridview1.RowCount; i++)
            {
                for (int j = 0; j < datagridview1.ColumnCount; j++)
                {
                    if (i % 2 == 0) datagridview1[j, i].Style.BackColor = Color.LightBlue;
                }
            }
        }

        private void Read_DB_Fill_Grid(string table_name, DataGridView datagridview1)
        {
            // 用数据库将表格填满
            try
            {
                DataSet ds = MainView.mysql.sql_search_database("Select * from "+table_name);
                int colnum = ds.Tables[0].Columns.Count;         // 列数
                datagridview1.ColumnCount = colnum;              // 设置列数
                int rownum = ds.Tables[0].Rows.Count;            // 行数
                datagridview1.RowCount = rownum;                 // 设置行数
                for (int i = 0; i < rownum; i++)
                {
                    for (int j = 0; j < colnum; j++)
                    {
                        datagridview1[j, i].Value = ds.Tables[0].Rows[i][j].ToString();     // 把那一行显示出来
                    }
                }
            }
            catch {
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    {
                        datagridview1[j, i].Value = "";     // 把那一行显示出来
                    }
                }   
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ADD_cargon_info view = new ADD_cargon_info();
            view.ShowDialog();
            Read_DB_Fill_Grid("cargoinfotable", dataGridView1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int selected_index = dataGridView1.SelectedCells[0].RowIndex;
                CargoinfoEdit view = new CargoinfoEdit();

                view.originid = dataGridView1[0, selected_index].Value.ToString();
                view.originname = dataGridView1[1, selected_index].Value.ToString();
                view.originnote = dataGridView1[2, selected_index].Value.ToString();
                view.Fill_Info();
                view.ShowDialog();
                Read_DB_Fill_Grid("cargoinfotable", dataGridView1);
            }
            catch { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int selected_index = dataGridView1.SelectedCells[0].RowIndex;
                DeleteCargoConfig view = new DeleteCargoConfig();
                view.originid = dataGridView1[0, selected_index].Value.ToString();
                view.ShowDialog();
                Read_DB_Fill_Grid("cargoinfotable", dataGridView1);
            }
            catch {
 
            }
        }
    }
}
